import * as OIMO from "oimo";
import {Clazz} from "../entity/Clazz";
import {LandBlock} from "../entity/LandBlock";

class PhysicalController extends Clazz {
    constructor() {
        super();
        this.pool = [];
        this.world = new OIMO.World({info: true, worldscale: 50});
    }

    update() {
        this.world.step();
    }

    add(obj) {
        let collisionGroupes = {
            group1: 1 << 0,  // 00000000 00000000 00000000 00000001
            group2: 1 << 1,  // 00000000 00000000 00000000 00000010
            group3: 1 << 2,  // 00000000 00000000 00000000 00000100
            all: 0xffffffff  // 11111111 11111111 11111111 11111111
        };
        let config = [10, 0.4, 0.2, collisionGroupes.group1, collisionGroupes.group2];
        this.pool.push(obj);
        const worldObj = physicalWorld.add({
            type: obj.physicalType == null ? "box" : obj.physicalType,
            size: this.toArray(obj.getSize()),
            pos: this.toArray(obj.position),
            move: !(obj instanceof LandBlock),
            world: this.world
        });
        this.pool.push(worldObj);
        return worldObj;
    }

    remove(obj) {
        const index = this.pool.indexOf(obj);
        if (index > -1) {
            this.pool.splice(index, 2);
        }
    }

    toArray(obj3D) {
        return [obj3D.x, obj3D.y, obj3D.z];
    }
}

export {PhysicalController}